import app, { Next } from '../app'
import { IGroupMessageEvent } from '../../interface/bot.interface'
import { groupMessageSplit, screepsGroupVerify } from '../middleware/filter'
import commandMap, { Command } from '../controller'

class Screeps {
    public run() {
        app.on('message.group', screepsGroupVerify, groupMessageSplit, async (e: IGroupMessageEvent, next: Next) => {
            // 根据不同的command执行不同的操作
            try {
                if (e.command in commandMap) {
                    const commandFn = commandMap[e.command as Command]
                    if (e.args[0] === 'help') {
                        if (commandFn.help) e.group.sendMsg(commandFn.help())
                        else e.group.sendMsg('该命令帮忙信息暂未提供')
                    } else {
                        commandMap[e.command as Command](e, next)
                    }
                }
            } catch (error) {
                console.warn('command error')
            }
        })
    }
}

export default new Screeps()
